#import "FeedbackPopupView.h"

@interface FeedbackPopupView () <UITextViewDelegate>

@property (nonatomic, strong) UIView *feedbackView;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *charCountLabel;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UIButton *submitButton;

@property (nonatomic, strong) UILabel *placeholderLabel;

@end

@implementation FeedbackPopupView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 设置背景颜色
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        [self setupFeedbackView];
    }
    return self;
}

- (void)setupFeedbackView {
    CGFloat width = self.frame.size.width - 80;
    CGFloat height = 365 * scaleH;
    
    self.feedbackView = [[UIView alloc] initWithFrame:CGRectMake(40, 203 * scaleH, width, height)];
    self.feedbackView.backgroundColor = [UIColor whiteColor];
    self.feedbackView.layer.cornerRadius = 20;
    self.feedbackView.layer.masksToBounds = YES;
    [self addSubview:self.feedbackView];
    
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"意见反馈";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont pingFangSCMediumWithSize:17];
    titleLabel.textColor = myRGBHex(0x333333);
    [self.feedbackView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(26);
        make.height.mas_equalTo(21);
    }];
    
    self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.closeButton setBackgroundImage:[UIImage imageNamed:@"x"] forState:UIControlStateNormal];
    [self.closeButton addTarget:self action:@selector(closeButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.feedbackView addSubview:self.closeButton];
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-16);
        make.centerY.equalTo(titleLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
    
    self.textView = [UITextView new];
    self.textView.delegate = self;
    self.textView.tintColor = [UIColor clearColor];
    self.textView.backgroundColor = myRGBHexAlpha(0xCCCCCC, 0.1);
    self.textView.layer.cornerRadius = 5.0;
    self.textView.font = [UIFont pingFangSCRegularWithSize:14];
    [self.feedbackView addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(72);
        make.leading.mas_equalTo(20);
        make.trailing.mas_equalTo(-20);
        make.height.mas_equalTo(175 * scaleH);
    }];
    
    self.placeholderLabel = [UILabel new];
    self.placeholderLabel.text = @"请输入内容";
    self.placeholderLabel.textColor = myRGBHex(0xC7C7C7);
    self.placeholderLabel.font = [UIFont pingFangSCRegularWithSize:14];
    [self.feedbackView addSubview:self.placeholderLabel];
    [self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(88 * scaleH);
        make.leading.mas_equalTo(38);
        make.height.mas_equalTo(17);
    }];
    
    self.charCountLabel = [UILabel new];
    self.charCountLabel.text = @"0/2000字";
    self.charCountLabel.textAlignment = NSTextAlignmentRight;
    self.charCountLabel.font = [UIFont pingFangSCRegularWithSize:11];
    self.charCountLabel.textColor = myRGBHex(0xC7C7C7);
    [self.feedbackView addSubview:self.charCountLabel];
    [self.charCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-37);
        make.top.mas_equalTo(221 * scaleH);
        make.height.mas_equalTo(15);
    }];
    
    self.submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.submitButton setTitle:@"提交" forState:UIControlStateNormal];
    [self.submitButton setBackgroundColor:myRGBHex(0xFF0AA1)];
    [self.submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.submitButton addTarget:self action:@selector(submitButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    self.submitButton.titleLabel.font = [UIFont pingFangSCMediumWithSize:16];
    self.submitButton.layer.cornerRadius = 22;
    [self.feedbackView addSubview:self.submitButton];
    [self.submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-33 * scaleH);
        make.height.mas_equalTo(44);
        make.centerX.mas_equalTo(0);
        make.width.mas_equalTo(285 * scaleW);
    }];
}

- (void)closeButtonTapped {
    [self removeFromSuperview];
}

- (void)submitButtonTapped {
    NSString *feedbackText = self.textView.text;
    if (feedbackText.length > 0) {
        NSLog(@"提交的反馈内容：%@", feedbackText);
        [self removeFromSuperview];
    } else {
        NSLog(@"请输入反馈内容");
    }
}

- (void)textViewDidChange:(UITextView *)textView {
    NSInteger charCount = textView.text.length;
    self.charCountLabel.text = [NSString stringWithFormat:@"%ld/2000字", charCount];
    
    // 如果有输入内容，隐藏站位文字
    if (charCount > 0) {
        self.placeholderLabel.hidden = YES;
    } else {
        self.placeholderLabel.hidden = NO;
    }
    
    // 限制最大字数为 2000 字
    if (charCount > 2000) {
        textView.text = [textView.text substringToIndex:2000];
        self.charCountLabel.text = @"2000/2000字";
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
}

@end
